package com.chaoyi.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Part;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;
import com.chaoyi.model.PrivatePlay;
import com.chaoyi.service.PrivatePlayService;

/**
 * 私播管理
 * 
 * @author wbf
 */
@Controller
@MultipartConfig
@RequestMapping("/privatePlay")
public class PrivatePlayController {
	@Resource
	PrivatePlayService privatePlayService;
	/**
	 * 初始化跳转页面
	 * 
	 * @param request
	 * @param response
	 * @return mod
	 */
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/privatePlay/privatePlayList");
		return mod;
	}

	/**
	 * 首页展示
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("privatePlayList")
	@ResponseBody
	public JSONObject privatePlayList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<PrivatePlay> list = privatePlayService.findList(offset, limit);
		int count = privatePlayService.findCount();
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	/**
	 *  新增私播内容跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addPrivatePlay_plus")
	public ModelAndView addPrivatePlay_plus(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/privatePlay/addPrivatePlay");
		return mod;
	}
	
	
	/**
	 * 保存新增私播内容
	 */
	@ResponseBody
	@RequestMapping("addPrivatePlay")
	public JSONObject addPrivatePlay(HttpServletRequest request, HttpServletResponse response, PrivatePlay privatePlay) {
		JSONObject json = new JSONObject();
		int number = privatePlayService.savePrivatePlay(privatePlay);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改私播内容
	 */
	@RequestMapping("/alertPrivatePlay")
	public ModelAndView alertPrivatePlay(HttpServletRequest request, HttpServletResponse response,int id) {
		ModelAndView mod = new ModelAndView("/privatePlay/alertPrivatePlay");
		PrivatePlay privatePlay = privatePlayService.findPrivatePlayOne(id);
		mod.addObject("obj",privatePlay);
		return mod;
	}
	
	/**
	 * 保存修改的私播信息
	 */
	@ResponseBody
	@RequestMapping("alertSavePrivatePlay")
	public JSONObject alertSavePrivatePlay(HttpServletRequest request, HttpServletResponse response, PrivatePlay privatePlay) {
		JSONObject json = new JSONObject();
		int number = privatePlayService.alertSavePrivatePlay(privatePlay);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 删除私播内容
	 */
	@ResponseBody
	@RequestMapping("deletePrivatePlay")
	public JSONObject deletePrivatePlay(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = privatePlayService.deletePrivatePlay(id);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	/**
	 * 新增私播头像上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyImg")
	@ResponseBody
	public JSONObject uploadClassifyImg(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String imgUrl = null;
		try{
			imgUrl = privatePlayService.uploadClassifyImg2WSCDN(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(imgUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", imgUrl).toJSON();
		return result;
	}
	/**
	 * 新增私播语音上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyAudio")
	@ResponseBody
	public JSONObject uploadClassifyAudio(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String fileUrl = null;
		try{
			fileUrl = privatePlayService.uploadClassifyAudio2WSCDN(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(fileUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", fileUrl).toJSON();
		return result;
	}
	
	
	
}
